package com.yisu.expressway.gas_station;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiDetailSearchOption;
import com.baidu.mapapi.search.poi.PoiIndoorResult;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.baidu.mapapi.search.poi.PoiSortType;
import com.sina.weibo.sdk.api.share.Base;
import com.yisu.expressway.R;
import com.yisu.expressway.fragment.BaseFragment;
import com.yisu.expressway.location.LocationInfo;
import com.yisu.expressway.location.LocationListener;
import com.yisu.expressway.location.LocationService;
import com.yisu.expressway.utils.ToastUtil;

import butterknife.Bind;
import butterknife.ButterKnife;

public class SearchMapFragment extends BaseFragment implements OnGetPoiSearchResultListener {

    @Bind(R.id.bd_map_view)
    protected MapView mMapView;

    private BaiduMap mBaiduMap;
    private PoiSearch mPoiSearch = null;

    public SearchMapFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View parentView = inflater.inflate(R.layout.fragment_search_map, container, false);
        ButterKnife.bind(this,parentView);
        mBaiduMap = mMapView.getMap();
        getCurrentLocation();
        return parentView;
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onDestroyView(){
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    private void getCurrentLocation(){
        showLoading();
        LocationService.getInstance().start(new LocationListener() {
            @Override
            public void onSuccess(LocationInfo info) {
                initMapView(info);
                searchNearbyGasStation(info);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                dismissLoading();
            }
        });
    }

    private void initMapView(LocationInfo info) {
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
        int zoomLevel = 13;

        LatLng point = new LatLng(info.getLatitude(), info.getLongitude());
        BitmapDescriptor bitmap = BitmapDescriptorFactory
                .fromResource(R.drawable.ic_current_location);

        OverlayOptions option = new MarkerOptions()
                .position(point)
                .icon(bitmap);

        MapStatus mMapStatus = new MapStatus.Builder()
                .target(point)
                .zoom(zoomLevel)
                .build();
        mBaiduMap.addOverlay(option);
        MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
        //改变地图状态
        mBaiduMap.setMapStatus(mMapStatusUpdate);
    }

    private void searchNearbyGasStation(LocationInfo info){
        if(mPoiSearch == null){
            mPoiSearch = PoiSearch.newInstance();
            mPoiSearch.setOnGetPoiSearchResultListener(this);
        }
        LatLng center = new LatLng(info.getLatitude(), info.getLongitude());
        PoiNearbySearchOption nearbySearchOption = new PoiNearbySearchOption().keyword("加油站"
                ).sortType(PoiSortType.distance_from_near_to_far).location(center)
                .radius(10000).pageNum(1);
        mPoiSearch.searchNearby(nearbySearchOption);
    }

    @Override
    public void onGetPoiResult(PoiResult result) {
        Log.d("OMG","==result==" + result.describeContents());
        dismissLoading();
        if (result == null || result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
            ToastUtil.displayShortToastMsg(getActivity(), "附近未找到加油站");
            return;
        }
        if (result.error == SearchResult.ERRORNO.NO_ERROR) {
            PoiOverlay overlay = new MyPoiOverlay(mBaiduMap);
            mBaiduMap.setOnMarkerClickListener(overlay);
            overlay.setData(result);
            overlay.addToMap();
            overlay.zoomToSpan();
        }
    }

    @Override
    public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {

    }

    @Override
    public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {

    }

    private class MyPoiOverlay extends PoiOverlay {

        public MyPoiOverlay(BaiduMap baiduMap) {
            super(baiduMap);
        }

        @Override
        public boolean onPoiClick(int index) {
            super.onPoiClick(index);
            PoiInfo poi = getPoiResult().getAllPoi().get(index);
            ToastUtil.displayLongToastMsg(getActivity(),poi.address);
//            mPoiSearch.searchPoiDetail((new PoiDetailSearchOption())
//                    .poiUid(poi.uid));
            return true;
        }
    }
}
